package cc.zy.base.hr.service.impl;


import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.common.utils.TreeUtil;
import cc.zy.base.hr.entity.HrDept;
import cc.zy.base.hr.entity.HrDeptTree;
import cc.zy.base.hr.mapper.HrDeptMapper;
import cc.zy.base.hr.service.IHrDeptService;
import cc.zy.base.system.entity.Dept;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 部门
 * Service
 *
 * @author cheng
 * @time 2022/03/08 20:21:56
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class HrDeptServiceImpl extends ServiceImpl<HrDeptMapper, HrDept> implements IHrDeptService {

    @Autowired
    private final HrDeptMapper deptMapper;

    /**
     * 分页查询部门
     * @param request QueryRequest
     * @param hrDept hrDept
     * @return
     */
    @Override
    public IPage<HrDept> findHrDepts(QueryRequest request, HrDept hrDept) {
        Page<HrDept> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countDeptDetail(hrDept));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findDeptDetailPage(page, hrDept);
    }

    /**
     * 增加
     * @param hrDept hrDept
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savedept(HrDept hrDept) {
        deptMapper.savedept(hrDept);
    }

    /**
     * 判断部门是否存在
     * @param hrDept hrDept
     * @return
     */
    @Override
    public boolean hrDeptExist(HrDept hrDept) {
        HrDept hrDept1 = deptMapper.hrDeptExist(hrDept);
        boolean flag = false;
        if(hrDept1 != null){
            flag = true;
        }
        return flag;
    }

    /**
     * 修改
     * @param hrDept
     */
    @Override
    public void updateHrDeptById(HrDept hrDept) {
        hrDept.setLastModifyDate(new Date());
        deptMapper.updateHrDeptById(hrDept);
    }

    /**
     * 用WXID查找真实姓名
     * @param userId
     * @return
     */
    @Override
    public String getNameByWxUserId(String userId) {
        return deptMapper.getNameByWxUserId(userId);
    }

    @Override
    public Integer getConstantId(String constant) {
        return deptMapper.getConstantId(constant);
    }

    @Override
    public void deleteByName(String hrDept) {
        deptMapper.deleteByName(hrDept);
    }

    @Override
    public List<HrDept> getAllHrDept() {
        return deptMapper.getAllHrDept();
    }

    @Override
    public Date getHrDeptUpdateDate() {
        return deptMapper.getHrDeptUpdateDate();
    }

    @Override
    public List<HrDept> findAll() {
        return deptMapper.findAll();
    }

    /**
     * @author 根据部门名称数组 来
     * @param deptNames
     * @return
     */
    @Override
    public List<HrDept> findDeptsByNames(String[] deptNames) {
        return deptMapper.findDeptsByNames(deptNames);
    }

    @Override
    public List<HrDeptTree<HrDept>> findDept() {
        List<HrDept> hrDepts = deptMapper.findHrDeptAll();

        List<HrDeptTree<HrDept>> hrDeptTrees = this.convertDept(hrDepts);
        return TreeUtil.buildHrDeptTree(hrDeptTrees);
    }

    private List<HrDeptTree<HrDept>> convertDept(List<HrDept> HrDept) {
        List<HrDeptTree<HrDept>> trees = new ArrayList<>();
        HrDept.forEach(hrDept -> {
            HrDeptTree<HrDept> tree = new HrDeptTree<>();
            tree.setId(String.valueOf(hrDept.getId()));
            tree.setParentId(String.valueOf(hrDept.getParentId()));
            tree.setName(hrDept.getDeptName());
            tree.setData(hrDept);
            trees.add(tree);
        });
        return trees;
    }
}
